package com.atguigu.gulimall.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import com.atguigu.common.exception.BizCodeEnume;
import com.atguigu.common.utils.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * Sentinel 配置
 */
@Configuration
public class SentinelGetewayConfig {
    public SentinelGetewayConfig(){
        GatewayCallbackManager.setBlockHandler((serverWebExchange, throwable) -> {
            R error = R.error(BizCodeEnume.TOOL_MANY_REQUEST.getCode(), BizCodeEnume.TOOL_MANY_REQUEST.getMsg());
            return ServerResponse.ok().body(Mono.just(JSON.toJSONString(error)), String.class);
        });
    }
}
